//
// Created by yangjian on 19-3-16.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link.c"
#include "httpd.h"
#include "link.h"

link_list_t * url_params_parse(char *);

int main(char ** argsc, int argv) {

    char str[] = "id=123&name=xiaoyang&email=906388445@qq.com&mobile=17855684475";
    link_list_t * link = url_params_parse(str);
    printf("[The size of link]: %d\n", link->size);
    printf("++++++++++++++++++ Traversing the link +++++++++++++++++++");
    link_list_node_t *node;
    param_t *param;
    node = link->head->next;
    while(node->next != NULL) {
        param = (param_t *)node->value;
        printf("[name]: %s\n", param->key);
        printf("[value]: %s\n", param->value);
        node = node->next;
    }
    return 0;
}

link_list_t * url_params_parse(char *str)
{
    if (str == NULL) {
        return NULL;
    }
    char *segment, *index;
    link_list_t *link;
    param_t *param;
    link = (link_list_t *) new_link_list();
    segment = strtok(str, "&");
    while (segment != NULL) {
        if ((index = strstr(segment, "=")) != NULL) {
            param = malloc(sizeof(param_t));
            memset(param, 0x00, sizeof(param_t));
            *index++ = '\0';
            param->key = index;
            param->value = segment;
            link_list_add(link, param);
        }
        segment = strtok(NULL, "&");
    }
    return link;
}
